<!-- -->
<template>
  <div class="box public">
    <el-card>
      <div class="Btn">
        <span
          v-for="(item,idx) in type"
          :key="idx"
          :class="active === idx ? 'active':''"
          @click="tab(idx,item.id)"
        >{{item.title}}</span>
      </div>
      <SearchTable
        @search="searchHouse"
        :page="searchPage"
        @cityData="searchCity"
        v-model="searchData"
      />
      <el-button class="add" @click="add">添加</el-button>
      <div class="table">
        <el-table style="width: 100%" :data="tableData" height="465px">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="小区名称" prop="housingName"></el-table-column>
          <el-table-column label="标题" prop="listingTitle"></el-table-column>
          <el-table-column label="面积" prop="houseArea"></el-table-column>
          <el-table-column label="总价" prop="sellingprice"></el-table-column>
          <el-table-column label="排序" prop="pv"></el-table-column>
          <el-table-column label="状态" prop="date">
            <template slot-scope="scope">
              <div v-if="scope.row.isShow === '1'">
                <el-switch v-model="value"></el-switch>
              </div>
              <div v-if="scope.row.isShow === '0'">
                <el-switch v-model="value1"></el-switch>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop="name" class="btn">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="page">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total*1"
          :current-page.sync="nowPage"
          @current-change="changePage"
        ></el-pagination>
      </div>

      <el-dialog :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="小区名称" prop="housingName">
            <el-input v-model="ruleForm.housingName"></el-input>
          </el-form-item>
          <el-form-item label="标题" prop="listingTitle">
            <el-input v-model="ruleForm.listingTitle"></el-input>
          </el-form-item>
          <el-form-item label="面积" prop="houseArea">
            <el-input v-model="ruleForm.houseArea"></el-input>
          </el-form-item>
          <el-form-item label="总价" prop="sellingprice">
            <el-input v-model="ruleForm.sellingprice"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="pv">
            <el-input v-model="ruleForm.pv"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="isShow">
            <el-input v-model="ruleForm.isShow"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import SearchTable from "@/components/SearchTable";
import {
  getOneCifyData,
  getTwoCifyData,
  page,
  addHouseList,
  delHouseList,
  searchHouseList
} from "@/api";
export default {
  // import引入到组件需要注入到对象中才能使用
  components: {
    SearchTable
  },
  data() {
    // 这里存放数据
    return {
      searchPage: [
        {
          lable: "地区",
          type: "city",
          value: {
            province: "province",
            city: "city",
            area: "area"
          }
        },
        {
          lable: "搜索",
          type: "input",
          value: "value",
          placeholder: "请输入关键字"
        }
      ],
      type: [
        {
          title: "新房",
          id: 1
        },
        {
          title: "二手房",
          id: 2
        },
        {
          title: "商铺",
          id: 3
        },
        {
          title: "写字楼",
          id: 4
        },
        {
          title: "租房",
          id: 5
        }
      ],
      num: 1,
      tableData: [],
      search: "",
      size: 10, //每页条数
      total: 0, //总条数
      searchData: {}, //输入框的内容
      nowPage: 1,
      id: 1,
      dialogVisible: false,
      ruleForm: {
        classifyType: this.id
      },
      value: true,
      value1: false,
      con: ""
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监听data中的数据变化
  // watch: {},
  // 方法集合
  methods: {
    searchHouse(data) {
      this.con = data.value;
      searchHouseList({
        value: this.con,
        classify: this.ruleForm.classifyType
      }).then(res => {
        this.tableData = res.data;
      });
      console.log(data);
    },
    // tab切换
    tab(idx, id) {
      this.active = idx;
      this.id = id;
      this.ruleForm.classifyType = id;
      page({
        size: this.size,
        current: this.nowPage,
        classify: this.id
      }).then(res => {
        console.log(res);
        this.total = res.total;
        this.tableData = res.data;
      });
    },
    // 点击分页的每一页 渲染对应数据
    changePage(e) {
      this.nowPage = e;
      this.tab(this.active, this.id);
    },
    // 控制弹框
    setDialog() {
      this.dialogVisible = !this.dialogVisible;
    },
    // 添加按钮
    add() {
      this.setDialog();
    },
    // 'X' 弹框的关闭
    handleClose(done) {
      this.$confirm("确认要关闭吗?")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 确认按钮
    sure() {
      addHouseList(this.ruleForm).then(res => {
        console.log(res);
        this.tab(this.active, this.id);
      });
      this.setDialog();
    },
    // 删除按钮
    del(id) {
      delHouseList({ id }).then(res => {
        this.tab(this.active, this.id);
        console.log(res);
      });
    },
    // 城市筛选数据
    searchCity(data) {
      this.tableData = data;
    }
  },
  //生命周期 - 创建完成 (可以访问当前this实例)
  created() {
    this.tab(0, 1);
  },
  //生命周期 - 挂载完成 (可以访问DOM元素)
  mounted() {}
};
</script> 

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang='scss'  scoped>
.box {
  padding: 20px 38px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.Btn {
  width: 100%;
  height: auto;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  span {
    padding: 10px 20px;
    border: 1px solid #ccc;
    background: steelblue;
    color: #fff;
  }
  span:nth-child(2) {
    border-left: none;
  }
  .active {
    background: #fff;
    color: steelblue;
  }
}
.table {
  margin-top: 15px;
  width: 100%;
  height: auto;
}
.page {
  width: 100%;
  height: auto;
  margin-top: 15px;
}
.add {
  margin-top: 10px;
}

.btn {
  display: flex;
}
</style>
